package org.vaadin.golfscoretracker;

import java.util.Locale;

import org.vaadin.appfoundation.i18n.Lang;
import org.vaadin.appfoundation.view.AbstractView;
import org.vaadin.appfoundation.view.ViewContainer;
import org.vaadin.appfoundation.view.ViewHandler;
import org.vaadin.golfscoretracker.ui.views.CoursesView;
import org.vaadin.golfscoretracker.ui.views.PlayersView;
import org.vaadin.golfscoretracker.ui.views.RoundsView;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

public class GolfScoreTrackerApplication extends Application implements
        ViewContainer {

    private static final long serialVersionUID = 2076425557022715957L;

    private final VerticalLayout mainLayout = new VerticalLayout();

    private AbstractView<?> currentView;

    @Override
    public void init() {
        ViewHandler.initialize(this);
        Lang.initialize(this);
        Lang.setLocale(Locale.ENGLISH);

        Window mainWindow = new Window("Golf Score Tracker");
        mainLayout.setSizeFull();
        mainWindow.setSizeFull();
        mainWindow.setContent(mainLayout);

        initButtons();
        registerViews();

        mainWindow.addComponent(new CoursesView());
        setMainWindow(mainWindow);

        ViewHandler.activateView(PlayersView.class);
    }

    private void initButtons() {
        Button roundsBtn = new Button(Lang.getMessage("rounds view"),
                new ClickListener() {

                    private static final long serialVersionUID = -215688234098145849L;

                    @Override
                    public void buttonClick(ClickEvent event) {
                        ViewHandler.activateView(RoundsView.class);
                    }
                });

        Button playersBtn = new Button(Lang.getMessage("players view"),
                new ClickListener() {

                    private static final long serialVersionUID = -215688234098145849L;

                    @Override
                    public void buttonClick(ClickEvent event) {
                        ViewHandler.activateView(PlayersView.class);
                    }
                });

        Button coursesBtn = new Button(Lang.getMessage("courses view"),
                new ClickListener() {

                    private static final long serialVersionUID = 1407911438280004726L;

                    @Override
                    public void buttonClick(ClickEvent event) {
                        ViewHandler.activateView(CoursesView.class);
                    }
                });

        HorizontalLayout layout = new HorizontalLayout();
        layout.addComponent(roundsBtn);
        layout.addComponent(playersBtn);
        layout.addComponent(coursesBtn);
        mainLayout.addComponent(layout);
    }

    private void registerViews() {
        ViewHandler.addView(PlayersView.class, this);
        ViewHandler.addView(CoursesView.class, this);
        ViewHandler.addView(RoundsView.class, this);
    }

    @Override
    public void activate(AbstractView<?> view) {
        if (currentView == null) {
            mainLayout.addComponent(view);
        } else {
            mainLayout.replaceComponent(currentView, view);
        }

        currentView = view;

        mainLayout.setExpandRatio(currentView, 1);
    }

    @Override
    public void deactivate(AbstractView<?> view) {
        // TODO Auto-generated method stub

    }
}
